#include <time.h>
#include "cftp.h"
#include "ftp.h"
#include <sstream>
using namespace std; 
int renameFtpFile(char *server, int port, char *usr, char *password, char *remoteName, char *newName)
{
    CFTPManager ftp;
    stringstream ftpServer;
    ftpServer << server << ":" << port;
    if (ftp.login2Server(ftpServer.str()) != FTP_SUCCESS)
    {
        cli_app_log(CLI_LOG_NOTICE, "ftp connect failed \n");
        return -1;
    }
    
    if (ftp.inputUserName(usr) != FTP_SUCCESS)
    {
        cli_app_log(CLI_LOG_NOTICE, "ftp usr is not correct \n");
        return -1;
    }

    if (ftp.inputPassWord(password) != FTP_SUCCESS)
    {
        cli_app_log(CLI_LOG_NOTICE, "ftp login is faied \n");
        return -1;
    }

    int32_t ret = FTP_SUCCESS;
    ret = ftp.Rename(remoteName, newName);
    if ( ret != FTP_SUCCESS)
    {
        cli_app_log(CLI_LOG_NOTICE, "ftp rename %s to %s failed ret is %d \n", remoteName, newName, ret);
        return ret;
    }
    
    if (ftp.quitServer() != FTP_SUCCESS)
    {
        cli_app_log(CLI_LOG_NOTICE, "quit ftp server not success \n");
    }

    return ret ;

}

int creatRenzixingFtpDir(char *server, int port, char *usr, char *password, char *type, FTPDIR *ftpDir)
{

    CFTPManager ftp;
    stringstream ftpServer;
    time_t uptime;
    time(&uptime);
    struct tm p = { 0 };
    localtime_r(&uptime, &p);
    char year[12] = {0};
    sprintf(year, "%04d%2d%02d",  (1900 + p.tm_year),
            (1 + p.tm_mon), p.tm_mday);
    char hour[4] = {0};
    sprintf(hour, "%02d", p.tm_hour);
    char min[4] = {0};
    sprintf(min, "%02d", (p.tm_min/5 * 5));
    ftpServer << server << ":" << port;
    if (ftp.login2Server(ftpServer.str()) != FTP_SUCCESS)
    {
        cli_app_log(CLI_LOG_NOTICE, "ftp connect failed \n");
        return -1;
    }
    
    if (ftp.inputUserName(usr) != FTP_SUCCESS)
    {
        cli_app_log(CLI_LOG_NOTICE, "ftp usr is not correct \n");
        return -1;
    }

    if (ftp.inputPassWord(password) != FTP_SUCCESS)
    {
        cli_app_log(CLI_LOG_NOTICE, "ftp login is faied \n");
        return -1;
    }

    int32_t ret = FTP_SUCCESS;
    if (strcmp(ftpDir->typeName, type) != 0)
    {
        ret = ftp.StatFileOrDir(type);
        if (ret == FTP_FAILED)
        {   
            return ret;
        }
        if (ret == FTP_SUCCESS)
        {
            ret = ftp.CreateDirectory(type);
            if ( ret != FTP_SUCCESS)
            {
                cli_app_log(CLI_LOG_NOTICE, "ftp create dir %s failed ret is %d \n", type, ret);
                return ret;
            }
        }
        strcpy(ftpDir->typeName, type);
        memset(ftpDir->year, 0, 12);
        memset(ftpDir->hour, 0, 4);
        memset(ftpDir->min, 0, 4);
    }
    
    if (strcmp(ftpDir->year, year) != 0)
    {
        char tmp[128] = {0};
        sprintf(tmp, "%s/%s", type, year);
        ret = ftp.StatFileOrDir(tmp);
        if (ret == FTP_FAILED)
        {   
            return ret;
        }
        if (ret == FTP_SUCCESS)
        {
            ret = ftp.CreateDirectory(tmp);
            if ( ret != FTP_SUCCESS)
            {
                cli_app_log(CLI_LOG_NOTICE, "ftp create dir %s failed ret is %d \n", tmp, ret);
                return ret;
            }
        }
        strcpy(ftpDir->year, year);
    }

    if (strcmp(ftpDir->hour, hour) != 0)
    {
        char tmp[128] = {0};
        sprintf(tmp, "%s/%s/%s", type, year, hour);
        ret = ftp.StatFileOrDir(tmp);
        if (ret == FTP_FAILED)
        {   
            return ret;
        }
        if (ret == FTP_SUCCESS)
        {
            ret = ftp.CreateDirectory(tmp);
            if ( ret != FTP_SUCCESS)
            {
                cli_app_log(CLI_LOG_NOTICE, "ftp create dir %s failed ret is %d \n", tmp, ret);
                return ret;
            }
            
        }
        strcpy(ftpDir->hour, hour);
    }

    if (strcmp(ftpDir->min, min) != 0)
    {
        char tmp[128] = {0};
        sprintf(tmp, "%s/%s/%s/%s", type, year, hour, min);
        ret = ftp.StatFileOrDir(tmp);
        if (ret == FTP_FAILED)
        {   
            return ret;
        }
        if (ret == FTP_SUCCESS)
        {
            ret = ftp.CreateDirectory(tmp);
            if ( ret != FTP_SUCCESS)
            {
                cli_app_log(CLI_LOG_NOTICE, "ftp create dir %s failed ret is %d \n", tmp, ret);
                return ret;
            }
        }
        strcpy(ftpDir->min, min);

    }


    if (ftp.quitServer() != FTP_SUCCESS)
    {
        cli_app_log(CLI_LOG_NOTICE, "quit ftp server not success \n");
    }

    return ret ;

}
